﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using FantasyBlog.DAL;

namespace FantasyBlog.MemeberShip
{
    public class BlogRoleProvider:RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            var roles = from r in ModelContainer.RoleSet
                        where roleNames.Contains(r.name.Trim())
                        select r;
            var users = from u in ModelContainer.UserSet
                        where usernames.Contains(u.username)
                        select u;
            foreach (var r in roles)
            {
                foreach (var u in users)
                {
                    if (!u.Roles.Contains(r))
                        u.Roles.Add(r);
                }
            }
            ModelContainer.SaveChanges();
        }

        public override string ApplicationName
        {
            get;
            set;
        }
        public override void CreateRole(string roleName)
        {
            var role = new Role();
            role.name = roleName;
            role.description = roleName;
            ModelContainer.RoleSet.Add(role);
            ModelContainer.SaveChanges();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            try
            {
                var role = ModelContainer.RoleSet.Find(roleName);
                if (role != null)
                {
                    ModelContainer.RoleSet.Remove(role);
                    ModelContainer.SaveChanges();
                }
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var role = ModelContainer.RoleSet.Find(roleName);
            if(role == null)
                return new string[]{};
            var userName = (from u in role.Users
                            select u.username).ToArray();
            return userName;

        }

        public override string[] GetAllRoles()
        {
            var roleName = (from r in ModelContainer.RoleSet
                            select r.name.Trim()).ToArray();
            return roleName;
        }

        public override string[] GetRolesForUser(string username)
        {
            try
            {
                var rolesName = (from r in
                                     (from u in ModelContainer.UserSet
                                      where u.username == username
                                      select u.Roles).FirstOrDefault()
                                 select r.name);
                var result = new List<string>();
                foreach (var n in rolesName)
                {
                    result.Add(n.Trim());
                }
                return result.ToArray();
            }
            catch
            {
                return new string[] { };
            }
                              
        }

        public override string[] GetUsersInRole(string roleName)
        {
            try
            {
                var users = (from u in
                                 (from r in ModelContainer.RoleSet
                                  where r.name.Trim() == roleName.Trim()
                                  select r.Users).FirstOrDefault()
                             select u.username).ToArray();
                return users;
            }
            catch
            {
                return new string[] { };
            }
            
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            try
            {
                var roles = (from u in ModelContainer.UserSet
                             where u.username.Trim() == username && u.Roles.Contains((
                                 from r in ModelContainer.RoleSet
                                 where r.name.Trim() == roleName.Trim()
                                 select r).FirstOrDefault())
                             select u.Roles).FirstOrDefault();
                if (roles == null)
                    return false;
                return true;
            }
            catch
            {

                return false;
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            var roles = from r in ModelContainer.RoleSet
                        where roleNames.Contains(r.name)
                        select r;
            var userRoles = from u in ModelContainer.UserSet
                            where usernames.Contains(u.username)
                            select u.Roles;
            foreach (var r in roles)
            {
                foreach (var u in userRoles)
                {
                    if (u.Contains(r))
                        u.Remove(r);
                }
            }
            ModelContainer.SaveChanges();
        }

        public override bool RoleExists(string roleName)
        {
            var role = (from r in ModelContainer.RoleSet
                        where r.name.Trim() == roleName.Trim()
                        select r).FirstOrDefault();
            return role != null;
        }
        BlogModelContainer ModelContainer = new BlogModelContainer();
    }
}